<?php
class props_init extends PHPUnit_Framework_TestCase{

    public function setUp()
    {
		$this->object = kernel::single('tbdata_api_tbapi');
		$this->cat_model = app::get('tbdata')->model('cats');
		$this->prop_model = app::get('tbdata')->model('props');
		$this->prop_values_model = app::get('tbdata')->model('prop_values');
		$this->fetch_props_model = app::get('tbdata')->model('fetch_props');
    }
	public function get_props($cid,$child_path=''){
		if($child_path){
			$params['child_path'] = $child_path;
		}
		$params['cid'] = $cid;
		$params['method'] = 'taobao.itemprops.get';
		$params['fields'] = 'pid,is_input_prop,parent_pid,parent_vid,name,must,multi,prop_values,is_key_prop,is_sale_prop,is_item_prop,status,sort_order,child_template,is_allow_alias';
		$props = $this->object->get_data($params);
		if($props['itemprops_get_response']['item_props']['item_prop']){
			$prop_array = $props['itemprops_get_response']['item_props']['item_prop'];
			foreach($prop_array as $prop){
				$prop['cid'] = $cid;
				$this->prop_model->save($prop);
				unset($prop['id']);
				if($prop['prop_values']['prop_value']){
					foreach($prop['prop_values']['prop_value'] as $prop_value){
						$prop_value['pid'] = $prop['pid'];
						$prop_value['cid'] = $cid;
						$prop_value['prop_name'] = $prop['name'];
						$this->prop_values_model->save($prop_value);
						unset($prop_value['id']);
						if($prop_value['is_parent']){
							$_child_path = $prop['pid'].':'.$prop_value['vid'];
							$this->get_props($cid,$_child_path);
						}
					}
				}
			}
		}
	}
    public function testGetcat(){

		$rows = $this->fetch_props_model->getList('cid',array('status'=>'waiting'),0,-1);
		$count = count($rows);
		$i = 1;
		foreach($rows as $row){
			echo "\n总数".$count.",已处理".$i++;
			$this->get_props($row['cid']);
		}
		

		print_r($cats);exit;

		
		/*
		$params['method'] = 'taobao.itemprops.get';
		$params['cid'] = 1403;
		$params['fields'] = 'pid,name,must,multi,prop_values,is_key_prop,is_sale_prop,is_item_prop,is_enum_prop';
		$a = $object->get_data($params);
		print_r($a);exit;
		
		*/
		/*
		$params['method'] = 'taobao.itemprops.get';
		$params['cid'] = 1403;
		$params['child_path'] = '20000:10752';
		$params['fields'] = 'pid,name,must,multi,prop_values,is_key_prop,is_sale_prop,is_item_prop';
		$a = $object->get_data($params);
		print_r($a);exit;
		*/
		
		/*
		$params['method'] = 'taobao.product.get';
		$params['cid'] = 1403;
		$params['child_path'] = '20000:10752;20225:23570';
		#$params['pid'] = '20000';
		#$params['parent_pid'] = '94544809';
		$params['fields'] = 'pid,name,must,multi,prop_values,is_key_prop,is_sale_prop,is_item_prop';
		$a = $object->get_data($params);
		print_r($a);exit;
		*/

		
		$params['method'] = 'taobao.products.search';
		$params['cid'] = 1403;
		$params['fields'] = 'product_id,name,pic_url,cid,props,binds_str,price,tsc';
		$a = $object->get_data($params);
		print_r($a);exit;
		/*
		$params['method'] = 'taobao.product.get';
		$params['cid'] = 1403;
		$params['product_id'] = 132096064;
		$params['fields'] = 'cat_name,props,binds,name,sale_props,price,desc,product_imgs,pic_url,pic_path';
		$a = $object->get_data($params);
		print_r($a);*/
		

	}
}
